<?php
// @formatter:off
/**
 * @file s.readrate.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-13 04:08:49 +0000 (Mon, 13 May 2013) $
 *
 * $Id: s.readrate.php 64 2013-05-13 04:08:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/services/s.readrate.php $
 */
// @formatter:on

$wcServiceStatus = false;
$wcServiceErrorCode = 500;

$wcPageCache->disabled();
if(defined('__SERVICE__') && $wcGroup && !$wcGroupForbidden && $wcProfile->allowedAny(WC_PERM_COMIC_SET_RATE, WC_PERM_COMIC_SET_READ)) {
	/*
	 * Condition:
	 *	- On GET method, if there's a comic specified ($wcComic) and the
	 *	  user is allowed to set as read and rate comics, the service
	 *	  returns the current user rate value and if the comic was marked
	 *	  as read by the user.
	 *	  Also, it returns the count of reads and the average rate,
	 *	  globally for the comic.
	 *	- On POST method, if there's a comic specified ($wcComic), the
	 *	  post parameter RATE is set, and the requesting user is allowed
	 *	  set comics rate, the service set the comic rate.
	 *	  Also, if the post parameter READ is set, and the requesting user
	 *	  is allowed mark comics as read, the service also marks the comic
	 *	  as read.
	 */
	if($_SERVER["REQUEST_METHOD"] == "GET") {
		if($wcComic && $wcProfile->allowedTo(WC_PERM_COMIC_SET_RATE, WC_PERM_COMIC_SET_READ)) {
			$wcServiceStatus = true;

			$rates = $wcReadRate->readRate($wcComic, $wcUser);
			$wcServiceXMLResult .= "<personal>";
			if($rates["read"]["read"]) {
				$wcServiceXMLResult .= "<read/>";
			}
			$wcServiceXMLResult .= "<rate>{$rates["rate"]["rate"]}</rate>";
			$wcServiceXMLResult .= "</personal>";

			$rates = $wcReadRate->comicReadRate($wcComic);
			$wcServiceXMLResult .= "<global>";
			$wcServiceXMLResult .= "<reads>{$rates["reads"]}</reads>";
			$wcServiceXMLResult .= "<rate>{$rates["rate"]}</rate>";
			$wcServiceXMLResult .= "</global>";
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	} elseif($_SERVER["REQUEST_METHOD"] == "POST") {
		if($wcComic && (isset($_REQUEST['RATE']) || isset($_REQUEST['READ']))) {
			if(isset($_REQUEST['READ']) && $wcProfile->allowedTo(WC_PERM_COMIC_SET_READ)) {
				if($wcReadRate->setRead($wcComic, $_REQUEST['READ'] == "Y", $wcUser)) {
					$wcServiceStatus = true;

					$rates = $wcReadRate->readRate($wcComic, $wcUser);
					$read = @$rates["read"]["read"];

					$wcServiceXMLResult .= "<personal>";
					if($read) {
						$wcServiceXMLResult .= "<read/>";
					}
					$wcServiceXMLResult .= "</personal>";
					$wcComic->rated = false;
				} else {
					$wcServiceErrorMsg = "Unable to set comic read status";
				}
			}
			if(isset($_REQUEST['RATE']) && $wcProfile->allowedTo(WC_PERM_COMIC_SET_RATE)) {
				if($wcReadRate->setRate($wcComic, $_REQUEST['RATE'], $wcUser)) {
					$wcServiceStatus = true;

					$rates = $wcReadRate->readRate($wcComic, $wcUser);
					$rate = @$rates["rate"]["rate"];

					$wcServiceXMLResult .= "<personal>";
					$wcServiceXMLResult .= "<rate>{$rate}</rate>";
					$wcServiceXMLResult .= "</personal>";
					$wcComic->rated = false;
				} else {
					$wcServiceErrorMsg = "Unable to set comic rate";
				}
			}
		} else {
			$wcServiceErrorMsg = "This service was call with invalid parameters";
		}
	}
} else {
	$wcServiceErrorCode = 403;
	$wcServiceErrorMsg = "Unable to access service";
}
?>
